//
// Created by fanghr on 18-3-29.
//

#include <cstddef>
#include "../include/ashmem_cpp.h"

ashmem_cpp::ashmem::ashmem(const char *name, size_t size) {
    _close = true;
    fd = ashmem_create_region(name, size);
    if (fd < 0)
        throw nullptr;
}

ashmem_cpp::ashmem::ashmem(int _fd) {
    _close = false;
    fd = _fd;
    if (fd < 0)
        throw nullptr;
}

ashmem_cpp::ashmem::~ashmem() {
    if (_close)
        close(fd);
}

int ashmem_cpp::ashmem::set_prot_region(int prot) {
    return ashmem_set_prot_region(fd, prot);
}

int ashmem_cpp::ashmem::pin_region(size_t offset, size_t len) {
    return ashmem_pin_region(fd, offset, len);
}

int ashmem_cpp::ashmem::unpin_region(size_t offset, size_t len) {
    return ashmem_unpin_region(fd, offset, len);
}

int ashmem_cpp::ashmem::get_size_region() {
    return ashmem_get_size_region(fd);
}

int ashmem_cpp::access::get_fd(ashmem_cpp::ashmem &obj) {
    return obj.fd;
}
